package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MaterialInfoMapper;
import com.ruoyi.system.domain.MaterialInfo;
import com.ruoyi.system.service.IMaterialInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 物料信息Service业务层处理
 *
 * @author tanshen
 * @date 2023-08-21
 */
@Service
public class MaterialInfoServiceImpl extends ServiceImpl<MaterialInfoMapper, MaterialInfo> implements IMaterialInfoService {
    @Autowired
    private MaterialInfoMapper materialInfoMapper;

    @Override
    @Transactional
    public boolean updateMaterial(String materialId, boolean isAdd, int num) {
        QueryWrapper<MaterialInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("material_id", materialId);
        List<MaterialInfo> list = materialInfoMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list) || list.size() > 1) {
            throw new BaseException("物料信息出入库", "物料信息不存在id:" + materialId);
        }
        MaterialInfo info = list.get(0);
        Integer inventoryQuantity = info.getInventoryQuantity();
        if (isAdd) {
            info.setInventoryQuantity(inventoryQuantity + num);
        } else {
            if (inventoryQuantity - num < 0) {
                throw new BaseException("物料信息出库", "物料库存不够id:" + materialId + "，当前库存：" + inventoryQuantity + "需要扣除库存：" + num);
            }
            info.setInventoryQuantity(inventoryQuantity - num);
        }
        int i = materialInfoMapper.updateMaterialInfo(info);
        return i > 0;
    }

    /**
     * 查询物料信息
     *
     * @param id 物料信息主键
     * @return 物料信息
     */
    @Override
    public MaterialInfo selectMaterialInfoById(Long id) {
        return materialInfoMapper.selectMaterialInfoById(id);
    }

    /**
     * 查询物料信息列表
     *
     * @param materialInfo 物料信息
     * @return 物料信息
     */
    @Override
    public List<MaterialInfo> selectMaterialInfoList(MaterialInfo materialInfo) {
        return materialInfoMapper.selectMaterialInfoList(materialInfo);
    }

    /**
     * 查询物料信息列表
     *
     * @param materialId 物料代码
     * @return 物料信息
     */
    @Override
    public List<MaterialInfo> selectMaterialList(String materialId, String supplierId) {
        if (StringUtils.isEmpty(materialId)) {
            return new ArrayList<>();
        }
        if (StringUtils.isEmpty(supplierId) || "-1".equals(supplierId)) {
            return materialInfoMapper.selectMaterialList(materialId);
        }
        return materialInfoMapper.selectMaterialInventoryList(materialId, supplierId);
    }

    /**
     * 新增物料信息
     *
     * @param materialInfo 物料信息
     * @return 结果
     */
    @Override
    public int insertMaterialInfo(MaterialInfo materialInfo) {
        return materialInfoMapper.insertMaterialInfo(materialInfo);
    }

    /**
     * 修改物料信息
     *
     * @param materialInfo 物料信息
     * @return 结果
     */
    @Override
    public int updateMaterialInfo(MaterialInfo materialInfo) {
        return materialInfoMapper.updateMaterialInfo(materialInfo);
    }

    /**
     * 批量删除物料信息
     *
     * @param ids 需要删除的物料信息主键
     * @return 结果
     */
    @Override
    public int deleteMaterialInfoByIds(String ids) {
        return materialInfoMapper.deleteMaterialInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除物料信息信息
     *
     * @param id 物料信息主键
     * @return 结果
     */
    @Override
    public int deleteMaterialInfoById(Long id) {
        return materialInfoMapper.deleteMaterialInfoById(id);
    }
}
